//
//  CBTNewVersionView.m
//  CBT
//
//  Created by Mr.Zhang on 2018/12/17.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "CBTNewVersionView.h"

@interface CBTNewVersionView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *upDateBtn;
@end

@implementation CBTNewVersionView

- (void)showVersionViewWithDic:(NSDictionary *)dic
{
    self.titleLabel.text = [NSString stringWithFormat:@"%@V%@",kLocalizedString(@"系统已检测到新版本",nil),[dic[@"data"]objectForKey:@"version_code"]];
    [self.upDateBtn setTitle:kLocalizedString(@"前去更新",nil) forState:(UIControlStateNormal)];
    [self showUpDateInfoWithArray:[dic[@"data"]objectForKey:@"update_msg"]];
    self.closeBtn.hidden = [[dic[@"data"]objectForKey:@"flag"]intValue] == 1;
}

- (void)dealloc{
    NSLog(@"[%@ dealloc]",[self class]);
}
- (IBAction)updataAction:(UIButton *)sender {
    if (self.UpDataClickBlock != nil) {
        self.UpDataClickBlock(2,self);
    }
}

- (IBAction)cancelAction:(UIButton *)sender {
    if (self.UpDataClickBlock != nil) {
        self.UpDataClickBlock(1,self);
    }
}

- (void)showUpDateInfoWithArray:(NSArray *)array
{
    
    UILabel *upDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(6, 13, 115, 15)];
    upDateLabel.textColor = UIColorFromRGB(0x666666);
    upDateLabel.font = [UIFont boldSystemFontOfSize:15];
    upDateLabel.text = kLocalizedString(@"更新内容:",nil);
    [self.scrollView addSubview:upDateLabel];
    
    NSMutableArray *labelArray = [NSMutableArray array];
    
    for (int i = 0; i < array.count; i++) {
        
        CGFloat height = [NSString sizeWithString:array[i] font:[UIFont systemFontOfSize:15] width: CGRectGetWidth(self.bounds)-40 height:0];
        
        UILabel *label = [[UILabel alloc]init];
        label.textColor = UIColorFromRGB(0x999999);
        label.font = [UIFont systemFontOfSize:15];
        label.tag = i;
        label.frame = CGRectMake(7, i  * (height + 16)+upDateLabel.bottom + 20, CGRectGetWidth(self.bounds)-40, height);
        label.text = array[i];
        [labelArray addObject:label];
        [self.scrollView addSubview:label];
    }
    
    UILabel *label = labelArray[array.count - 1];
    
    self.scrollView.contentSize = CGSizeMake(0, label.bottom + 16);
}

@end
